package org.example.leetCode;

public class strStr {
    public static void main(String[] args) {
        String haystack = "mississippi", needle = "issipi";
        System.out.println(strStr1(haystack, needle));
    }

    public static int strStr1(String haystack, String needle) {
        if (needle.length()>haystack.length()){
            return -1;
        }else if (needle.equals("")){
            return 0;
        }else if (haystack.equals(needle)){
            return 0;
        }
        for (int i = 0; i <= haystack.length()-needle.length(); i++) {
            if (haystack.charAt(i) == needle.charAt(0)) {
                if (isEqual(haystack, needle, i)) {
                    return i;
                }
            }
        }
        return -1;
    }

    public static boolean isEqual(String haystack, String needle, int i) {
        return haystack.substring(i, i + needle.length()).equals(needle);
    }
}
